﻿using System;
using System.Windows;
using System.Windows.Controls;
using WeatherPrediction;

namespace WeatherPredictionWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {

        #region Members

        ParseForecast weatherForecast = new ParseForecast();
        string Apiformat ="json";
        int rowNum, colNum;

        #endregion Members

        #region Constant



        #endregion Constant

        #region Construct
        public MainWindow()
        {
            InitializeComponent();
        }
        #endregion Construct

        #region delegate

        #endregion delegate


        #region Methods
        private void btSearch_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                UpdateView();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Service not aviable now you are probably offline"+ex.Message);
            }
        }

        private void UpdateView()
        {
            weatherForecast = SendRequestToWebservice(tbLocation.Text);
            DisplayTheDataOnTheView(weatherForecast.forecastDay.forecastDay.listForecastDay);
        }



        
        private ParseForecast SendRequestToWebservice(string location)
        {
            //get country code and name from the location 
            string urlToGetLanguageCode = "http://autocomplete.wunderground.com/aq?query=" + location;
            ParseResult countryCode = Communication.MakeRequestCountryCode(urlToGetLanguageCode, Apiformat);

            //read the result
            Communication.GetResponseOfRequestCountryCode(countryCode);

            string countryEnglishName = Communication.GetResponseOfRequestCountryName(countryCode);
            string languageCodes = getLanguageCode(countryEnglishName);

            //get wheather forecast for the state 
            // the url look like this http://api.wunderground.com/api/46656caf5b865813/forecast/lang:DL/q/Germany/Nuremberg.json 
            string urlForForcast = "http://api.wunderground.com/api/46656caf5b865813/forecast/lang:" + languageCodes + "/q/" + countryEnglishName + "/" + location + Apiformat;
            ParseForecast wheatherforecast = Communication.MakeRequestForecast(urlForForcast);

            //read the result of the request
            weatherForecast = Communication.GetResponseOfRequestForecast(wheatherforecast);
            return weatherForecast;
        }

        private string getLanguageCode(string code)
        {
            if (code == "Germany")
            {
                return "DL";
            }
            if (code == "France")
            {
                return "FR";
            }
            return "EN";
        }

        private void DisplayTheDataOnTheView(forecastOfOneday[] forecastOfOneday)
        {
            gridDay.Children.Clear();
            for (int i = 0; i < forecastOfOneday.Length; i++)
            {
                getDataFromCurrForCast(i, rowNum, colNum, forecastOfOneday[i], gridDay);
            }
        }


        private void getDataFromCurrForCast(int i, int rowNum, int colNum, forecastOfOneday currForcast, Grid updatedGrid)
        {
            var newUserCont = new UserControlWeatherDay(currForcast.title, currForcast.icon, currForcast.fcttext, currForcast.fcttext_metric, currForcast.pop, currForcast.icon_url);

            rowNum = i / 2;
            colNum = i % 2;

            updatedGrid.Children.Add(newUserCont);
            Grid.SetColumn(newUserCont, colNum);
            Grid.SetRow(newUserCont, rowNum);
            
        }

        #endregion Methods

        private void rbJson_Checked(object sender, RoutedEventArgs e)
        {
            Apiformat = ".json"; 
        }

        private void rbXml_Checked(object sender, RoutedEventArgs e)
        {
            Apiformat = ".xml";
        }
    }
}
